//
//  ViewControllerImpl+clt.m
//  wecloudservice
//
//  Created by gaoguangxiao on 2022/12/23.
//  Copyright © 2022 高广校. All rights reserved.
//  集合视图

#import "ViewControllerImpl+clt.h"
#import <objc/runtime.h>
#import "CGCollectionViewCell.h"
#import "WLCltViewLayout.h"
#import "UIView+LTEmptyView.h"
static char wl_cltKey;

@interface ViewControllerImpl (clt)<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,WLCltViewLayoutDelegate>

@end

@implementation ViewControllerImpl (clt)

- (void)setWl_collectionView:(UICollectionView *)wl_collectionView{
    objc_setAssociatedObject(self, &wl_cltKey, wl_collectionView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UICollectionView *)wl_collectionView {
    return objc_getAssociatedObject(self, &wl_cltKey);
}


- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)setInitclt:(BOOL)initclt {
    if (initclt) {
        WLCltViewLayout *layout = [[WLCltViewLayout alloc] init];
        layout.delegate = self;
//        float itemWideh = (SCREEN_WIDTH - 36*Device_Ccale) /2;
//        layout.estimatedItemSize = CGSizeMake(itemWideh, 178);
        self.wl_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
        self.wl_collectionView.dataSource = self;
        self.wl_collectionView.delegate = self;
        self.wl_collectionView.showsVerticalScrollIndicator = NO;
        self.wl_collectionView.showsHorizontalScrollIndicator = NO;
        self.wl_collectionView.backgroundColor = [UIColor colorWithHexString:@"#F7F7F7"];
        [self.wl_collectionView registerClass:CGCollectionViewCell.class forCellWithReuseIdentifier:@"CGCollectionViewCell_ID"];
        [self.view addSubview:self.wl_collectionView];
        [self.wl_collectionView showEmptyView];
        [self.wl_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.bottom.right.mas_equalTo(0);
        }];
        
        //如果类实现下拉刷新的方法，就添加下拉逻辑
        if ([self respondsToSelector:@selector(refreshView)]) {
            self.wl_collectionView.mj_header = [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshView)];
        }
        if ([self respondsToSelector:@selector(loadMore)]) {
            self.wl_collectionView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
        }
        if (@available(iOS 11.0, *)) {
            self.wl_collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
//            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    } else {
        
    }
}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    CGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CGCollectionViewCell_ID" forIndexPath:indexPath];
    return cell;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGSize)waterFlowLayout:(id)Layout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    float itemWideh = (SCREEN_WIDTH - 36*Device_Ccale) /2;
    return CGSizeMake(itemWideh, indexPath.row %2 == 0 ? 252*Device_Ccale : 252*Device_Ccale);
}

// 两列cell之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    CGFloat minimumInteritem = 10 * Device_Ccale;
//    NSLog(@"最小间距%f",minimumInteritem);
    return minimumInteritem;
//    return 100;
}
@end
